package com.zhuyiyi.mmall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhuyiyi.mmall.entity.Cart;
import com.zhuyiyi.mmall.entity.Product;
import com.zhuyiyi.mmall.entity.User;
import com.zhuyiyi.mmall.mapper.ProductMapper;
import com.zhuyiyi.mmall.service.impl.CartServiceImpl;
import com.zhuyiyi.mmall.service.impl.UserAddressServiceImpl;
import com.zhuyiyi.mmall.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 11
 * @since 2021-05-10
 */
@Controller
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartServiceImpl cartService;
    @Autowired
    private UserAddressServiceImpl userAddressService;
    @Autowired
    private ProductMapper productMapper;

    @GetMapping("/add/{productId}/{price}/{quantity}")
    public String add(
            @PathVariable("productId") Integer productId,
            @PathVariable("price") Float price,
            @PathVariable("quantity") Integer quantity,
            HttpSession session
    ){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("product_id",productId);
        Cart findCart=cartService.getOne(wrapper);
        if (findCart==null) {
            Cart cart = new Cart();
            cart.setProductId(productId);
            cart.setQuantity(quantity);
            cart.setCost(price * quantity);
            User user = (User) session.getAttribute("user");

            try {
                cart.setUserId(user.getId());
                if (cartService.save(cart)) {
                    return "redirect:/cart/findAllCart";
                }
            } catch (Exception e) {
                return "redirect:/product/findById/" + productId;
            }
        }
        else{
            findCart.setQuantity(findCart.getQuantity()+quantity);
            findCart.setCost(findCart.getCost()+price*quantity);
            User user = (User) session.getAttribute("user");

            try {
                findCart.setUserId(user.getId());

                if (cartService.updateById(findCart)) {
                    Product product = productMapper.selectById(findCart.getProductId());
                    Integer stock = product.getStock() - quantity;
                    product.setStock(stock);
                    productMapper.updateById(product);
                    return "redirect:/cart/findAllCart";
                }
            } catch (Exception e) {
                return "redirect:/product/findById/" + productId;
            }}


        return null;
}

    @GetMapping("/findAllCart")
    public ModelAndView findAllCart(HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("settlement1");
        User user = (User)session.getAttribute("user");
        modelAndView.addObject("cartList",cartService.findAllCartVOByUserId(user.getId()));
        return modelAndView;
    }


    @GetMapping("/deleteById/{id}")
    public String deleteById(@PathVariable("id") Integer id){
        cartService.removeById(id);
        return "redirect:/cart/findAllCart";
    }

    @PostMapping("/updateCart/{id}/{quantity}/{cost}")
    @ResponseBody
    public String updateCart(
            @PathVariable("id") Integer id,
            @PathVariable("quantity") Integer quantity,
            @PathVariable("cost") Float cost
    ){
        Cart cart = cartService.getById(id);
        cart.setQuantity(quantity);
        cart.setCost(cost);
        if(cartService.updateById(cart)){
            return "success";
        }else{
            return "fail";
        }
    }

    @GetMapping("/settlement2")
    public ModelAndView settlement2(HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("settlement2");
        User user = (User)session.getAttribute("user");
        QueryWrapper wrapper=new QueryWrapper();
        if (user!=null){
        wrapper.eq("user_id",user.getId());
        modelAndView.addObject("addressList",userAddressService.list(wrapper));}
        modelAndView.addObject("cartList",cartService.findAllCartVOByUserId(user.getId()));
        return modelAndView;
    }




}

